import { request } from '@umijs/max';
import { setAuthHeader } from '../utils/authInterceptor';

/**
 * 获取用户列表（分页）
 * @param params 查询参数
 * @returns 用户列表数据
 */
export async function getUserListPaginated(params: USER.UserListParams) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request<USER.UserListResponse>('/api/user/pagedList', {
    method: 'GET',
    headers,
    params,
  });
}

/**
 * 添加用户
 * @param params 用户信息
 * @returns 添加结果
 */
export async function addUser(params: Omit<USER.UserModel, 'id' | 'createTime'>) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/auth/register', {
    method: 'POST',
    headers,
    data: params,
  });
}

/**
 * 更新用户信息
 * @param params 用户信息
 * @returns 更新结果
 */
export async function updateUser(params: Partial<USER.UserModel> & { id: number }) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/user/update', {
    method: 'PUT',
    headers,
    data: params,
  });
}

/**
 * 删除用户
 * @param id 用户ID
 * @returns 删除结果
 */
export async function removeUser(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/user/delete/${id}`, {
    method: 'DELETE',
    headers,
  });
}